package com.vegetable.modules.controller.stock;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vegetable.common.utils.PageUtils;
import com.vegetable.common.utils.Query;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.stock.CostAllocationRecord;
import com.vegetable.modules.service.stock.ICostAllocationRecordService;
import com.vegetable.modules.vo.stock.CostAllocationRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/stock/costAllocationRecord")
@AllArgsConstructor
@Api(value = "成本分摊记录", tags = "成本分摊记录")
public class CostAllocationRecordController extends AbstractController {
    
    private ICostAllocationRecordService costAllocationRecordService;
    
    /**
     * 分页查询成本分摊记录列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询成本分摊记录列表", notes = "分页查询成本分摊记录列表")
    public R list(@RequestParam Map<String, Object> params) {
        log.info("分页查询成本分摊记录列表，参数：{}", JSON.toJSONString(params));
        
        // 使用SQL联表查询直接获取包含商品信息的数据
        IPage<CostAllocationRecordVO> page = costAllocationRecordService.pageWithGoodsInfo(
            new Query<CostAllocationRecordVO>().getPage(params),
            params
        );
        
        return R.data(new PageUtils(page));
    }
    
    /**
     * 根据ID查询成本分摊记录详情
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据ID查询成本分摊记录详情", notes = "根据ID查询成本分摊记录详情")
    public R info(@PathVariable("id") Long id) {
        log.info("根据ID查询成本分摊记录详情，ID：{}", id);
        CostAllocationRecord record = costAllocationRecordService.getById(id);
        return R.data(record);
    }
    
    /**
     * 根据原始库存ID查询相关的分摊记录
     */
    @GetMapping("/listByOriginalStockId")
    @ApiOperation(value = "根据原始库存ID查询相关的分摊记录", notes = "根据原始库存ID查询相关的分摊记录")
    public R listByOriginalStockId(@RequestParam Long originalStockId) {
        log.info("根据原始库存ID查询相关的分摊记录，原始库存ID：{}", originalStockId);
        return R.data(costAllocationRecordService.listByOriginalStockId(originalStockId));
    }
    
    /**
     * 根据分摊批次号查询相关的分摊记录
     */
    @GetMapping("/listByAllocationBatchNo")
    @ApiOperation(value = "根据分摊批次号查询相关的分摊记录", notes = "根据分摊批次号查询相关的分摊记录")
    public R listByAllocationBatchNo(@RequestParam String allocationBatchNo) {
        log.info("根据分摊批次号查询相关的分摊记录，分摊批次号：{}", allocationBatchNo);
        return R.data(costAllocationRecordService.listByAllocationBatchNo(allocationBatchNo));
    }
    
    /**
     * 删除成本分摊记录
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除成本分摊记录", notes = "删除成本分摊记录")
    public R delete(@RequestBody Long[] ids) {
        log.info("删除成本分摊记录，ID列表：{}", JSON.toJSONString(ids));
        boolean result = costAllocationRecordService.removeByIds(Arrays.asList(ids));
        return result ? R.success("删除成功") : R.fail("删除失败");
    }
}